package com.ruoyi.web.controller.ydz;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.DiseaseNew;
import com.ruoyi.web.domain.KeyAndValue;
import com.ruoyi.web.service.IDiseaseNewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 疾病管理Controller
 * 
 * @author drx
 * @date 2023-02-01
 */
@Controller
@Api(tags = "疾病管理")
@RequestMapping({"/new/disease","/api/new/disease"})
public class DiseaseNewController extends BaseController
{
    private String prefix = "ydz/disease/new";

    @Autowired
    private IDiseaseNewService diseaseService;

    @Resource
    private RedisCache redisCache;

    @RequiresPermissions("new:disease:view")
    @GetMapping()
    public String disease()
    {
        return prefix + "/disease";
    }

    /**
     * 查询疾病管理树列表
     */
    @PostMapping("/list")
    @ResponseBody
    public List<DiseaseNew> list(DiseaseNew disease)
    {
        List<DiseaseNew> list = diseaseService.selectDiseaseList(disease);
        return list;
    }

    /**
     * 导出疾病管理列表
     */
    @RequiresPermissions("new:disease:export")
    @Log(title = "疾病管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DiseaseNew disease)
    {
        List<DiseaseNew> list = diseaseService.selectDiseaseList(disease);
        ExcelUtil<DiseaseNew> util = new ExcelUtil<DiseaseNew>(DiseaseNew.class);
        return util.exportExcel(list, "疾病管理数据");
    }

    /**
     * 新增疾病管理
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("disease", diseaseService.selectDiseaseById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存疾病管理
     */
    @RequiresPermissions("new:disease:add")
    @Log(title = "疾病管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody DiseaseNew disease)
    {
        return toAjax(diseaseService.insertDisease(disease));
    }

    /**
     * 修改疾病管理
     */
    @RequiresPermissions("new:disease:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DiseaseNew disease = diseaseService.selectDiseaseById(id);
        mmap.put("disease", disease);
        return prefix + "/edit";
    }

    /**
     * 修改保存疾病管理
     */
    @RequiresPermissions("new:disease:edit")
    @Log(title = "疾病管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody DiseaseNew disease)
    {
        return toAjax(diseaseService.updateDisease(disease));
    }

    /**
     * 删除
     */
    @RequiresPermissions("new:disease:remove")
    @Log(title = "疾病管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(diseaseService.deleteDiseaseById(id));
    }

    /**
     * 选择疾病管理树
     */
    @GetMapping(value = { "/selectDiseaseTree/{id}", "/selectDiseaseTree/" })
    public String selectDiseaseTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("disease", diseaseService.selectDiseaseTree(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载疾病管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = diseaseService.selectDiseaseTree();
        return ztrees;
    }

    /**
     * 查询疾病管理下拉
     */
    @RequestMapping ("/diseaseKeyAndValue")
    @ResponseBody
    @ApiOperation("获取疾病列表")
    @ApiImplicitParam(name = "pid", value = "父级项目id（科目层pid传0；疾病层传对应科目id）",required = true)
    public List<KeyAndValue> diseaseKeyAndValue(Long pid)
    {   DiseaseNew disease=new DiseaseNew();
        disease.setPid(pid);
        List<KeyAndValue> list = diseaseService.diseaseKeyAndValue(disease);
        return list;
    }
    /**
     * 查询疾病管理下拉
     */
    @PostMapping("/diseaseKeyValue")
    @ResponseBody
    @ApiOperation("根据id查询疾病名称，不传则查询所有")
    @ApiImplicitParam(name = "id", value = "疾病ID")
    public List<KeyAndValue> diseaseKeyValue(Long id)
    {   DiseaseNew disease=new DiseaseNew();
        disease.setId(id);
        List<KeyAndValue> list = diseaseService.diseaseKeyValue(disease);
        return list;
    }

    /**
     * 查询疾病列表，手机端
     * @author: drx
     * @date: 2023/6/7 9:52
     * @param: disease 疾病bean
     * @return: 疾病集合
     **/
    @RequestMapping("/getDiseaseList")
    @ResponseBody
    public List<DiseaseNew> getDiseaseList(@RequestBody DiseaseNew disease)
    {
        List<DiseaseNew> list = diseaseService.selectDiseaseList(disease);
        return list;
    }


    @GetMapping("selectDiseaseData")
    @ResponseBody
    public List<DiseaseNew> selectDiseaseData()
    {
        List<DiseaseNew> cacheList = redisCache.getCacheList("new:disease:anyDisease");
        return cacheList;
    }

    /**
     * 获取疾病信息
     */
    @GetMapping("/getDiseaseById/{id}")
    @ResponseBody
    public AjaxResult getDiseaseById(@PathVariable("id") Long id)
    {
        AjaxResult ajaxResult=new AjaxResult();
        try{
            DiseaseNew disease = diseaseService.selectDiseaseById(id);
            ajaxResult.put("data",disease);
            ajaxResult.put("code","1");
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取疾病详情查询失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /*
    * 真正的树状疾病->科目信息
    * */
    @RequestMapping("/getDiseaseData")
    @ResponseBody
    public AjaxResult getDiseaseData(@RequestBody DiseaseNew disease){

        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<Map<String, Object>> diseaseData = diseaseService.getDiseaseData(disease);
            ajaxResult.put("data",diseaseData);
            ajaxResult.put("code","1");
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取疾病详情查询失败，请联系管理员！");
        }
        return ajaxResult;
    }



}
